<?php
/**
 * @TODO 壹深圳视频上传到VMS,调用VMS视频上传接口进行素材上传
 * @url https://vms.mp.sztv.com.cn/vms/servlet/BlUploadServlet?siteid=863&TenantID=ysz
 * @author JackieLee
 * @
 */

require_once ("./class/H.class.php");
require_once ("./class/Aes.class.php");
require_once ("./class/Snoopy.class.php");
$uploadUrl = "https://vms.mp.sztv.com.cn/vms/servlet/BlUploadServlet?siteid=863&TenantID=ysz";
$transcodingUrl = "https://vms.mp.sztv.com.cn/vms/MakingTranscodeFtpUploadServlet";
$getVideoInfoUrl = "https://vms.mp.sztv.com.cn/vms/APIServiceReceiver";
$snoopy = new Snoopy();
$authUser = [
    //老有才
    'lyc' => 'uLZpwQeboXylGBTIrzdaOtjHqcCVYWAkDEShvKMfmUnNRF'
];

$appId = isset($_GET['appId']) && !empty($_GET['appId']) ? H::sqlxss($_GET['appId']) : "";
$appSercet = isset($_GET['appSercet']) && !empty($_GET['appSercet']) ? H::sqlxss($_GET['appSercet']) : "";
$action = isset($_GET['action']) && !empty($_GET['action']) ? H::sqlxss($_GET['action']) : "";


!$appId && die(H::json_encode_ch(['state'=>false,'msg'=>"appID is Empty"]));;
!$appSercet && die(H::json_encode_ch(['state'=>false,'msg'=>"appSercet is Empty"]));
!$action && die(H::json_encode_ch(['state'=>false,'msg'=>"action is Empty"]));

if($authUser[$appId] != $appSercet){
    die(H::json_encode_ch(['state'=>false,'msg'=>"error appid or appsercet"]));
}

if(!in_array($action,['upload','trans','info'])){
    die(H::json_encode_ch(['state'=>false,'msg'=>"action is a invalid value"]));
}

if($action == "upload"){
    $file = $_FILES['file'];        //文件信息
    $filename = $file['name'];      //本地文件名
    $tmpFile = $file['tmp_name'];   //临时文件名
    $fileType = $file['type'];      //文件类型
    $returnData = H::postUploadFile($uploadUrl, $filename, $tmpFile, $fileType );

    $data = json_decode($returnData,1);

    if($data['state'] == true){
        //上传成功，自动进行转码
        $title = "老友才视频征集作品";
        $description = "老友才视频征集作品-".time().rand(10000,999999);
        $contentinfo ='[{"videourl": "'.$data['data'][0]['url'].'","catalogId": "227","description": "'.$description.'", "title": "'.$title.'","transcodeGroup": "1","tags": "老有才"}]';

        $urlParams = "method=ftpUploadVideo&partnerToken=106e84f8ea1a455f32a2370cf594c671&isPublish=1&contentinfo=".urlencode($contentinfo);

        $snoopy->fetch($transcodingUrl.'?'.$urlParams);

        $transCodingArr = json_decode($snoopy->getResults(),1);
        if($transCodingArr['interfaceStatus'] == 1){
            die(H::json_encode_ch([
                'state'=>true,
                'msg'=>"success",
                'data'=>[
                    'transCodingId' => $transCodingArr['conJa'][0]['id'],
                    'RequestId'=> Aes::aesEn($data['data'][0]['url']),
                    'FileName'=>$data['data'][0]['filename'],
                    'Url' => $data['data'][0]['url'],
                    'Time' => $data['data'][0]['time'],
                    'Message' => $transCodingArr['conJa']['message']
                ]
            ]));
        } else {
            die(H::json_encode_ch([
                'state'=>true,
                'msg'=>"success",
                'data'=>['transCodingId' => "", 'RequestId'=> Aes::aesEn($data['data'][0]['url']), 'FileName'=>$data['data'][0]['filename'],
                    'Url' => $data['data'][0]['url'], 'Time' => $data['data'][0]['time'], 'Message' => $transCodingArr['conJa']['message']
                ]
            ]));
        }
    } else {
        die(H::json_encode_ch(['state'=>false,'msg'=>$data['message']]));
    }

} else if($action == "trans"){

    $requestId = isset($_GET['requestId']) && !empty($_GET['requestId']) ? H::sqlxss($_GET['requestId']) : "";
    !$requestId && die(H::json_encode_ch(['state'=>false,'msg'=>"requestId is Empty"]));

    $title = "老友才视频征集作品";
    $description = "老友才视频征集作品-".time().rand(10000,999999);
    $contentinfo ='[{"videourl": "'.Aes::aesDe($requestId).'","catalogId": "227","description": "'.$description.'", "title": "'.$title.'","transcodeGroup": "1","tags": "老有才"}]';

    $urlParams = "method=ftpUploadVideo&partnerToken=106e84f8ea1a455f32a2370cf594c671&isPublish=1&contentinfo=".urlencode($contentinfo);

    $snoopy->fetch($transcodingUrl.'?'.$urlParams);

    $transCodingArr = json_decode($snoopy->getResults(),1);
    if($transCodingArr['interfaceStatus'] == 1){
        die(H::json_encode_ch([
            'state'=>true,
            'msg'=>"success",
            'data'=>[
                'transCodingId' => $transCodingArr['conJa']['id'],
                'RequestId'=> $requestId
            ]
        ]));
    } else {
        die(H::json_encode_ch(['state'=>false,'msg'=>"请求转码失败，请重试"]));
    }

} else if($action == "info"){
    $transCodingId = isset($_GET['transCodingId']) && !empty($_GET['transCodingId']) ? H::sqlxss($_GET['transCodingId']) : "";

    !$transCodingId && die(H::json_encode_ch(['state'=>false,'msg'=>"transCodingId is Empty"]));

    $urlParams = "method=getVideoById&partnerToken=106e84f8ea1a455f32a2370cf594c671&dataType=json&videoId=".$transCodingId;


    $snoopy->fetch($getVideoInfoUrl.'?'.$urlParams);

    $getVideoInfoArr = json_decode($snoopy->getResults(),1);

    if(!empty($getVideoInfoArr['video'])){
        $returnVideo = $getVideoInfoArr['video'][0];
        die(H::json_encode_ch([
            'state'=>true,
            'msg'=>"success",
            'data'=>[
                'transCodingId' => $returnVideo['id'],
                'formats' => [
                    'vodAddress'=> [
                        'definition' => 'HD',
                        'sourceType' => $returnVideo['vodAddress']['sourceType'],
                        'playUrl' => $returnVideo['vodAddress']['host'].$returnVideo['vodAddress']['clips'][0]['urls'][0]
                    ],
                    'tsAddress' => [
                        'definition' => 'HD',
                        'sourceType' => $returnVideo['tsAddress']['sourceType'],
                        'playUrl' => $returnVideo['tsAddress']['host'].$returnVideo['tsAddress']['clips'][0]['urls'][0]
                    ],
                    'mp4Address' => [
                        'definition' => 'HD',
                        'sourceType' => $returnVideo['mp4Address']['sourceType'],
                        'playUrl' => $returnVideo['mp4Address']['host'].$returnVideo['mp4Address']['clips'][0]['urls'][0]
                    ],
                ],
                'sourceMediaInfo' => $returnVideo['sourceMediaInfo'],
                'createTime' => $returnVideo['createTime'],
                'imagePath' => $returnVideo['imagePath'],
                'catalogPath' => $returnVideo['catalogPath'][0],

            ]
        ]));
    } else {
        die(H::json_encode_ch(['state'=>false,'msg'=>"data is empty"]));
    }
} else {
    die(H::json_encode_ch(['state'=>false,'msg'=>"action is a invalid value"]));
}









